module I18n
  def self.ts(*arr)
    j = (I18n.locale.to_s == 'en' ? ' ' : '')
    arr.flatten.map { |a| a.is_a?(Class) ? a.model_name.human : I18n.t(a) }.join(j)
  end
end

class NilClass
  def label
    ''
  end

  def split(*_arg)
    []
  end

  def format_date(_type)
    ''
  end
end

class Date
  def full_with_dot
    strftime('%Y.%m.%d')
  end
end

class Time
  def label
    strftime('%Y-%m-%d %H:%M:%S')
  end

  def first_day_of_the_same_month
    Time.parse("#{year}-#{month}-01")
  end

  def format_date(type)
    case type
    when :full
      strftime('%Y-%m-%d %H:%M:%S')
    when :min
      strftime('%Y-%m-%d %H:%M')
    when :date
      strftime('%Y-%m-%d')
    when :date_with_dot
      strftime('%Y.%m.%d')
    when :mon
      strftime('%Y-%m')
    when :day
      strftime('%Y年%m月%d日')
    when :month
      strftime('%Y年%m月')
    when :year
      strftime('%Y')
    when :quarter
      [strftime('%Y'), '年', [%w[第一 第二 第三 第四][(month - 1) / 3], '季度']].join
    end
  end

  def inspect
    label
  end

  def self.start_of_today
    new.strftime('%Y-%m-%d 00:00')
  end

  def self.end_of_today
    new.strftime('%Y-%m-%d 23:59')
  end
end

class Enumerize::Attribute
  def options_for_search
    options.map { |o| [o[0], (instance_variable_get(:@klass).to_s + '::' + name.to_s.upcase).constantize[o[1].to_sym]] }
  end
end

class Hash
  # hash = {aa: 1, bb: 2}
  # hash.to_obj
  #=> {aa: 1, bb: 2}
  # hash.aa
  #=> 1
  def to_object
    keys.each do |key|
      define_singleton_method key.to_sym do
        self[key]
      end

      define_singleton_method "#{key}=".to_sym do |v|
        self[key] = v
      end
    end
    self
  end
  alias to_obj to_object

  def merge_with_zero(arr)
    arr.each do |id|
      self[id] = 0 if self[id].blank?
    end
    self
  end
end
